package com.lxl.bt.view;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.aigestudio.wheelpicker.WheelPicker;
import com.lxl.bt.R;

import java.util.List;

/**
 * @Author : xinlong.lv
 */

public class SinglePickerDialog extends Dialog implements View.OnClickListener, WheelPicker.OnItemSelectedListener {

    private View rootView;
    private Context context;
    private TextView txtTitleCancel, txtTitleSure;
    private WheelPicker wheelPicker;

    private DialogOnClickListener dialogOnClickListener;
    private int position;
    private String string = "";

    public SinglePickerDialog(Context context) {
        super(context, R.style.DialogPickerTheme);

        rootView = LayoutInflater.from(this.getContext()).inflate(R.layout.layout_picker_dialog, null);
        txtTitleCancel = rootView.findViewById(R.id.txt_dialog_title_cancel);
        txtTitleSure = rootView.findViewById(R.id.txt_dialog_title_sure);
        wheelPicker = rootView.findViewById(R.id.wheel_picker);

        txtTitleCancel.setOnClickListener(this);
        txtTitleSure.setOnClickListener(this);
        wheelPicker.setOnItemSelectedListener(this);

        this.setCanceledOnTouchOutside(true);
        setContentView(rootView);

        this.context = context;

    }

    /**
     * 显示选择器
     *
     * @param strings
     */
    public void showPickerDialog(List<String> strings) {
        if (strings.size() > 0) {
            string = strings.get(0);
        }

        wheelPicker.setData(strings);

        Window dialogWindow = this.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        dialogWindow.setWindowAnimations(R.style.dialog_animation);

        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();

        WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(layoutParams);
        this.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_dialog_title_cancel:
                dismiss();
                break;
            case R.id.txt_dialog_title_sure:
                if (dialogOnClickListener != null) {
                    dialogOnClickListener.dialogOnClickListener(position, string);
                }
                dismiss();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemSelected(WheelPicker picker, Object data, int position) {
        this.position = position;
        string = String.valueOf(data);
    }


    public interface DialogOnClickListener {
        void dialogOnClickListener(int position, String string);
    }


    public void setDialogOnClickListener(DialogOnClickListener dialogOnClickListener) {
        this.dialogOnClickListener = dialogOnClickListener;
    }
}
